package com.mioto.ld.module.template.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.aop.anno.BizLogAnno;
import com.mioto.ld.module.log.model.BizModuleName;
import com.mioto.ld.module.log.model.BizOperationType;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.template.model.NotifyTemplate;
import com.mioto.ld.module.template.service.INotifyTemplateService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-23 19:35:50
 */
@RestController
@RequestMapping("notifyTemplate")
@Api(tags = "通知模板管理")
public class NotifyTemplateController {
    @Resource
    private INotifyTemplateService notifyTemplateService;

    @GetMapping
    @ApiOperation(value="查询通知模板列表",response = NotifyTemplate.class)
    public ResultData list (HttpServletRequest request,NotifyTemplate notifyTemplate){
        return ResultData.success(notifyTemplateService.findList(notifyTemplate));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询通知模板",response = NotifyTemplate.class)
    @BizLogAnno(moduleName = BizModuleName.NOTIFY,operationType = BizOperationType.PAGER)
    public ResultData pager (HttpServletRequest request, NotifyTemplate notifyTemplate,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<NotifyTemplate> list = notifyTemplateService.findList(notifyTemplate);
        PageInfo<NotifyTemplate> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询通知模板",response = NotifyTemplate.class)
    @BizLogAnno(moduleName = BizModuleName.NOTIFY,operationType = BizOperationType.DETAIL)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(notifyTemplateService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增通知模板")
    @BizLogAnno(moduleName = BizModuleName.NOTIFY,operationType = BizOperationType.INSERT)
    public ResultData add (NotifyTemplate notifyTemplate){
        return Optional.of(notifyTemplateService.insert(notifyTemplate))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(notifyTemplate))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新通知模板 局部更新,忽略空值")
    @BizLogAnno(moduleName = BizModuleName.NOTIFY,operationType = BizOperationType.UPDATE)
    public ResultData updateIgnoreNull (NotifyTemplate notifyTemplate){
        return Optional.of(notifyTemplateService.updateIgnoreNull(notifyTemplate))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(notifyTemplate))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新通知模板 整体更新")
    public ResultData update (NotifyTemplate notifyTemplate){
        return Optional.of(notifyTemplateService.update(notifyTemplate))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(notifyTemplate))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除通知模板")
    @BizLogAnno(moduleName = BizModuleName.NOTIFY,operationType = BizOperationType.DELETE)
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(notifyTemplateService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除通知模板")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(notifyTemplateService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }
}